//each node know the basic information of its demand,generator, onehop neighbor
#include "stdafx.h"
#include "common.h"
#include <stdlib.h>
#include <time.h>

void nodeConfigure(node *Node){
	//srand((unsigned)time(NULL));

	//node 1
    Node[0].demand_nums = 1;
	//Node[0].demand_limit[0].down = 2 + rand()%4;
	//Node[0].demand_limit[0].up = 25 + rand()%5;
	//Node[0].demand_para[0] = 1 + rand()%3;
	Node[0].demand_limit[0].down = 3;
	Node[0].demand_limit[0].up = 52;
    Node[0].demand_para[0] = 20;


	Node[0].generator_nums = 1;
	//Node[0].generator_limit[0].up =  40 + rand()%10;
	//Node[0].generator_limit[0].down = 0;
	//Node[0].generator_para[0] = 0.01*(1+rand()%9);
	Node[0].generator_limit[0].up =  10;
	Node[0].generator_limit[0].down = 0;
	Node[0].generator_para[0] = 0.09*10;

	Node[0].ipaddress = 0;
	//Node[0].totalSusc = 3;

	Node[0].oneHopNbr_nums = 3;
	Node[0].oneHopNbr_IP[0] = 1;
	Node[0].oneHopNbr_IP[1] = 4;
	Node[0].oneHopNbr_IP[2] = 5;
	Node[0].oneHopNbr_Susc[0] = 1;
	Node[0].oneHopNbr_Susc[1] = 1;
	Node[0].oneHopNbr_Susc[2] = 1;
	Node[0].oneHopNbr_flow_limit[0].up = 10;
	Node[0].oneHopNbr_flow_limit[0].down = -10;
	Node[0].oneHopNbr_flow_limit[1].up = 10;
	Node[0].oneHopNbr_flow_limit[1].down = -10;
	Node[0].oneHopNbr_flow_limit[2].up = 10;
	Node[0].oneHopNbr_flow_limit[2].down = -10;
   
	//node 2
    Node[1].demand_nums = 1;
	//Node[1].demand_limit[0].down = 2 + rand()%4;
	//Node[1].demand_limit[0].up = 25 + rand()%5;
	//Node[1].demand_para[0] = 1 + rand()%3;
	Node[1].demand_limit[0].down = 2;
	Node[1].demand_limit[0].up = 53;
	Node[1].demand_para[0] = 10;

	Node[1].generator_nums = 1;
	//Node[1].generator_limit[0].up =  40 + rand()%10;
	//Node[1].generator_limit[0].down = 0;
	//Node[1].generator_para[0] = 0.01*(1+rand()%9);
	Node[1].generator_limit[0].up =  47;
	Node[1].generator_limit[0].down = 0;
	Node[1].generator_para[0] = 0.03*10;

	Node[1].ipaddress = 1;
	//Node[1].totalSusc = 3;

	Node[1].oneHopNbr_nums = 3;
	Node[1].oneHopNbr_IP[0] = 0;
	Node[1].oneHopNbr_IP[1] = 2;
	Node[1].oneHopNbr_IP[2] = 3;
	Node[1].oneHopNbr_Susc[0] = 1;
	Node[1].oneHopNbr_Susc[1] = 1;
	Node[1].oneHopNbr_Susc[2] = 1;
	Node[1].oneHopNbr_flow_limit[0].up = 10;
	Node[1].oneHopNbr_flow_limit[0].down = -10;
	Node[1].oneHopNbr_flow_limit[1].up = 10;
	Node[1].oneHopNbr_flow_limit[1].down = -10;
	Node[1].oneHopNbr_flow_limit[2].up = 10;
	Node[1].oneHopNbr_flow_limit[2].down = -10;

	//node 3
    Node[2].demand_nums = 1;
	//Node[2].demand_limit[0].down = 2 + rand()%4;
	//Node[2].demand_limit[0].up = 25 + rand()%5;
	//Node[2].demand_para[0] = 1 + rand()%3;
	Node[2].demand_limit[0].down = 1;
	Node[2].demand_limit[0].up = 52;
	Node[2].demand_para[0] = 20;

	Node[2].generator_nums = 1;
	//Node[2].generator_limit[0].up =  40 + rand()%10;
	//Node[2].generator_limit[0].down = 0;
	//Node[2].generator_para[0] = 0.01*(1+rand()%9);
	Node[2].generator_limit[0].up =  47;
	Node[2].generator_limit[0].down = 0;
	Node[2].generator_para[0] = 0.08*10;

	Node[2].ipaddress = 2;
	//Node[2].totalSusc = 2;

	Node[2].oneHopNbr_nums = 2;
	Node[2].oneHopNbr_IP[0] = 1;
	Node[2].oneHopNbr_IP[1] = 3;
	Node[2].oneHopNbr_Susc[0] = 1;
	Node[2].oneHopNbr_Susc[1] = 1;
	Node[2].oneHopNbr_flow_limit[0].up = 10;
	Node[2].oneHopNbr_flow_limit[0].down = -10;
	Node[2].oneHopNbr_flow_limit[1].up = 10;
	Node[2].oneHopNbr_flow_limit[1].down = -10;

	//node 4
    Node[3].demand_nums = 1;
	//Node[3].demand_limit[0].down = 2 + rand()%4;
	//Node[3].demand_limit[0].up = 25 + rand()%5;
	//Node[3].demand_para[0] = 1 + rand()%3;
	Node[3].demand_limit[0].down = 1;
	Node[3].demand_limit[0].up = 54;
	Node[3].demand_para[0] = 30;

	Node[3].generator_nums = 1;
	//Node[3].generator_limit[0].up =  40 + rand()%10;
	//Node[3].generator_limit[0].down = 0;
	//Node[3].generator_para[0] = 0.01*(1+rand()%9);
	Node[3].generator_limit[0].up =  7;
	Node[3].generator_limit[0].down = 0;
	Node[3].generator_para[0] = 0.01*10;

	Node[3].ipaddress = 3;
	//Node[3].totalSusc = 3;

	Node[3].oneHopNbr_nums = 3;
	Node[3].oneHopNbr_IP[0] = 1;
	Node[3].oneHopNbr_IP[1] = 2;
	Node[3].oneHopNbr_IP[2] = 4;
	Node[3].oneHopNbr_Susc[0] = 1;
	Node[3].oneHopNbr_Susc[1] = 1;
	Node[3].oneHopNbr_Susc[2] = 1;
	Node[3].oneHopNbr_flow_limit[0].up = 10;
	Node[3].oneHopNbr_flow_limit[0].down = -10;
	Node[3].oneHopNbr_flow_limit[1].up = 10;
	Node[3].oneHopNbr_flow_limit[1].down = -10;
	Node[3].oneHopNbr_flow_limit[2].up = 10;
	Node[3].oneHopNbr_flow_limit[2].down = -10;

	//node 5
    Node[4].demand_nums = 1;
	//Node[4].demand_limit[0].down = 2 + rand()%4;
	//Node[4].demand_limit[0].up = 25 + rand()%5;
	//Node[4].demand_para[0] = 1 + rand()%3;
	Node[4].demand_limit[0].down = 5;
	Node[4].demand_limit[0].up = 54;
	Node[4].demand_para[0] = 30;

	Node[4].generator_nums = 1;
	//Node[4].generator_limit[0].up =  40 + rand()%10;
	//Node[4].generator_limit[0].down = 0;
	//Node[4].generator_para[0] = 0.01*(1+rand()%9);
	Node[4].generator_limit[0].up =  45;
	Node[4].generator_limit[0].down = 0;
	Node[4].generator_para[0] = 0.05*10;

	Node[4].ipaddress = 4;
	//Node[4].totalSusc = 3;

	Node[4].oneHopNbr_nums = 3;
	Node[4].oneHopNbr_IP[0] = 0;
	Node[4].oneHopNbr_IP[1] = 3;
	Node[4].oneHopNbr_IP[2] = 5;
	Node[4].oneHopNbr_Susc[0] = 1;
	Node[4].oneHopNbr_Susc[1] = 1;
	Node[4].oneHopNbr_Susc[2] = 1;
	Node[4].oneHopNbr_flow_limit[0].up = 10;
	Node[4].oneHopNbr_flow_limit[0].down = -10;
	Node[4].oneHopNbr_flow_limit[1].up = 10;
	Node[4].oneHopNbr_flow_limit[1].down = -10;
	Node[4].oneHopNbr_flow_limit[2].up = 10;
	Node[4].oneHopNbr_flow_limit[2].down = -10;

	//node 6
    Node[5].demand_nums = 1;
	//Node[5].demand_limit[0].down = 2 + rand()%4;
	//Node[5].demand_limit[0].up = 25 + rand()%5;
	//Node[5].demand_para[0] = 1 + rand()%3;
	Node[5].demand_limit[0].down = 4;
	Node[5].demand_limit[0].up = 51;
	Node[5].demand_para[0] = 30;

	Node[5].generator_nums = 1;
	//Node[5].generator_limit[0].up =  40 + rand()%10;
	//Node[5].generator_limit[0].down = 0;
	//Node[5].generator_para[0] = 0.01*(1+rand()%9);
	Node[5].generator_limit[0].up =  48;
	Node[5].generator_limit[0].down = 0;
	Node[5].generator_para[0] = 0.06*10;

	Node[5].ipaddress = 5;
	//Node[5].totalSusc = 6;

	Node[5].oneHopNbr_nums = 2;
	Node[5].oneHopNbr_IP[0] = 0;
	Node[5].oneHopNbr_IP[1] = 4;
	Node[5].oneHopNbr_Susc[0] = 3;
	Node[5].oneHopNbr_Susc[1] = 3;
	Node[5].oneHopNbr_flow_limit[0].up = 20;
	Node[5].oneHopNbr_flow_limit[0].down = -20;
	Node[5].oneHopNbr_flow_limit[1].up = 20;
	Node[5].oneHopNbr_flow_limit[1].down = -20;
}


void initPkt(node &Node, pkt &packet){
	packet.ipaddress = Node.ipaddress;
	packet.oneHopNbr_nums = Node.oneHopNbr_nums;

}

void parseInitPkt(node &Node, pkt *packet)
{
	unsigned int i,nbrIP;
	 
	for (i=0; i<Node.oneHopNbr_nums; i++){
		nbrIP = Node.oneHopNbr_IP[i];
		Node.oneHopNbr_nbrnums[i] = packet[nbrIP].oneHopNbr_nums;
	}
}


void nodeInit(node &Node){
	unsigned int i;

	//compute the weight value that is used to estimate the form of residual function
	Node.form_weight = 1;
	for (i=0; i<Node.oneHopNbr_nums; i++){
		Node.oneHopNbr_form_weight[i] = 1/(1+max(Node.oneHopNbr_nums,Node.oneHopNbr_nbrnums[i]));
		Node.form_weight -= Node.oneHopNbr_form_weight[i];
	}

	for (i=0; i<Node.oneHopNbr_nums; i++){ 
		if (Node.ipaddress < Node.oneHopNbr_IP[i]) {
			Node.oneHopNbr_flow_direction[i] = 1;
		}
		else {
			Node.oneHopNbr_flow_direction[i] = -1;
		}
	}
}